909D - Colorful Points - CodeForces Solution


data structures greedy implementation *2100

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

s = list(input().rstrip())
u, v = [], []
c, l = 0, s[0]
for i in s:
    if l ^ i:
        u.append(c)
        v.append(l)
        c, l = 0, i
    c += 1
u.append(c)
v.append(l)
ans = 0
while len(u) > 1:
    ans += 1
    u[0] += 1
    u[-1] += 1
    u0, v0 = [], []
    for i, j in zip(u, v):
        c = i - 2
        if c <= 0:
            continue
        if not v0 or v0[-1] ^ j:
            u0.append(c)
            v0.append(j)
        else:
            u0[-1] += c
    u, v = u0, v0
print(ans)

C++ Code:

#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define For(i,a,b) for(int i=a;i<b;i++)
#define Forn(i,a,b) for(int i=a;i>=0;i--)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define LL long long
#define first(x) get<0>(x)
#define second(x) get<1>(x)
#define third(x) get<2>(x)
using namespace std;

 
void solve(){
    string s;
    cin>>s;
    int n=s.length();
    
    vector<pair<int,int>> vec;
    int cnt=1;
    for(int i=1;i<n;i++){
        if(s[i]==s[i-1]){
            cnt++;
        }
        else{
            vec.pb({s[i-1]-'a',cnt});
            cnt=1;
        }
    }
    vec.pb({s[n-1]-'a',cnt});
    
    vector<pair<int,int>> new_vec;
    int ans=0;
    while(true){
        int sz=vec.size();
        /*
        for(auto p:vec){
            cout<<p.first<<" "<<p.second<<endl;
        }
        cout<<endl;
        */
        if(sz<=1) break;
        ans++;
        for(int i=0;i<sz;i++){
            if(i==0 || i==sz-1){
                vec[i].second--;
            }
            else vec[i].second-=2;
        }
        /*
        cout<<"here2"<<endl;
        for(auto p:vec){
            cout<<p.first<<" "<<p.second<<endl;
        }
        cout<<endl;
        */
        int i=0, j=1, cnt=0;
        while(i<sz){
            if(vec[i].second>0){
                cnt=vec[i].second;
                break;
            }
            i++;
        }
        //cout<<"i="<<i<<endl;
        if(i==sz){
            break;
        }
        j=i+1;
        while(j<sz){
            if(vec[j].second>0){
                if(vec[j].first==vec[i].first){
                    cnt+=vec[j].second;
                }
                else{
                    new_vec.pb({vec[i].first,cnt});
                    i=j;
                    cnt=vec[j].second;
                }
            }
            j++;
        }
        new_vec.pb({vec[i].first,cnt});
        
        vec=new_vec;
        new_vec.clear();
    }
    cout<<ans<<endl;
}
 

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	
	solve();
	
    return 0;
}


Comments

Submit
0 Comments
More Questions

Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations